package com.ihd.ihardware.common.widget;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.ihd.ihardware.R;

public class CircleView extends View {
    private PaintFlagsDrawFilter mPaintFlagsDrawFilter;
    private Paint mPaint;
    private int[] mColors = {Color.parseColor("#6F6AF0"), Color.parseColor("#6F6AF0"), Color.parseColor("#936AE5"),};

    private int mWidth; // 控件的宽高
    private int mHeight;

    private int mProgressWidth;

    private Context mContext;

    public CircleView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initAttr(attrs, defStyleAttr);

        init();
    }

    private void initAttr(AttributeSet attrs, int defStyleAttr) {
        TypedArray a = mContext.getTheme().obtainStyledAttributes(attrs, R.styleable.SportProgressView, defStyleAttr, 0);
        int n = a.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = a.getIndex(i);
            switch (attr) {
                case R.styleable.SportProgressView_progressWidth:
                    mProgressWidth = a.getDimensionPixelSize(attr, 14);
                    break;
                default:
                    break;
            }
        }
        a.recycle();
    }

    /**
     * 初始化
     */
    private void init() {
        mPaintFlagsDrawFilter = new PaintFlagsDrawFilter
                (0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStrokeWidth(mProgressWidth);
        mPaint.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);

        if (widthSpecMode == MeasureSpec.EXACTLY) {
            mWidth = widthSpecSize;
        } else if (widthSpecMode == MeasureSpec.AT_MOST) {
            mWidth = 500;
        }

        if (heightSpecMode == MeasureSpec.EXACTLY) {
            mHeight = heightSpecSize;
        } else if (widthSpecMode == MeasureSpec.AT_MOST) {
            mHeight = 500;
        }

        setMeasuredDimension(mWidth, mHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.setDrawFilter(mPaintFlagsDrawFilter);
        RectF rect = new RectF(mProgressWidth / 2 + getPaddingLeft(), mProgressWidth / 2 + getPaddingTop(), mWidth - mProgressWidth / 2, mHeight - mProgressWidth / 2);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        canvas.drawArc(rect, 0, 360, false, mPaint);

        LinearGradient linearGradient = new LinearGradient(getMeasuredWidth(), getMeasuredHeight(), mWidth, 0, mColors,
                null, LinearGradient.TileMode.CLAMP);
        mPaint.setShader(linearGradient);
        canvas.drawArc(rect, 0, 360, false, mPaint);
    }
}